﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ClassLibrary.BLL;

namespace WindowsFormsApplication1
{
    public partial class QuanLySanPham : Form
    {
        public QuanLySanPham()
        {
            InitializeComponent();
        }
        List<BLLSanPham> sanphams = new List<BLLSanPham>();
        List<BLLSanPham> sanphamtodelete = new List<BLLSanPham>();
        
        List<BLLLoaiSP> loaisp = new List<BLLLoaiSP>();
        private void QuanLySanPham_Load(object sender, EventArgs e)
        {
            loaisp = new BLLLoaiSP().GetAll();
            cbLoaiSP.DataSource = loaisp;
            cbLoaiSP.DisplayMember="TenLoai";
            cbLoaiSP.ValueMember = "IDLoai";
            cbLoaiSP.Text="Chọn loại";
           // cbLoaiSP.SelectedIndex = 0;

            GVSP.AutoGenerateColumns = false;
            sanphams = new BLLSanPham().GetAll();
            GVSP.DataSource = sanphams;
            btnxoa.Enabled = false;
            btnsua.Enabled = false;
        
        }

        private void GVSP_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            try
            {
                BLLSanPham sp = sanphams[e.RowIndex];
                txtmasp.Text = sp.MaSP;
                txttensp.Text = sp.TenSP;
                txtmausac.Text = sp.MauSac;
                txtchatlieu.Text = sp.ChatLieu;
                txtgia.Text = sp.Gia;
                txthinh.Text = sp.HinhAnh;
                cbLoaiSP.SelectedIndex = Convert.ToInt32(sp.Idloai)-1;
                btnthem.Enabled = false;
                btnsua.Enabled = true;
                btnxoa.Enabled = true;
                txtmasp.ReadOnly = true;
            }
            catch
            { }
        }

        private void btnreset_Click(object sender, EventArgs e)
        {
            txtmasp.Text = "";
            txttensp.Text = "";
            txtmausac.Text = "";
            txtchatlieu.Text = "";
            txtgia.Text = "";
            txthinh.Text = "";
            cbLoaiSP.SelectedIndex =0;
            btnthem.Enabled = true;
            btnsua.Enabled = false;
            btnxoa.Enabled = false;
            txtmasp.ReadOnly = false;
        }

        private void btnthem_Click(object sender, EventArgs e)
        {
            
            BLLSanPham sp = new BLLSanPham(-1,txtmasp.Text,cbLoaiSP.SelectedIndex,txttensp.Text,txtchatlieu.Text,txtmausac.Text,Convert.ToDecimal(txtgia.Text),txthinh.Text);
            sanphams.Add(sp);
            GVSP.DataSource = null;
            GVSP.DataSource = sanphams;
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            foreach (BLLSanPham sp in sanphams)
            {
                try
                {
                    if (sp.ID == -1)
                    {
                        string idsp = sp.Add();
                    }
                    if (sp.updated)
                    {
                        sp.Update();
                    }
                }
                catch
                {
                    string s = String.Format("Bị lỗi ở địa điểm ({0}-{1})",
                     sp.MaSP,sp.Gia);
                    MessageBox.Show(s, "Lỗi");

                    return;
                }
            }

            foreach (BLLSanPham sp in sanphamtodelete)
            {
                sp.Delete();
            }
        }

        private void btnxoa_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in GVSP.SelectedRows)
            {
                BLLSanPham sp = (BLLSanPham)row.DataBoundItem;

                sanphams.Remove(sp);
                sanphamtodelete.Add(sp);
                GVSP.DataSource = null;
                GVSP.DataSource = sanphams;
            }
        }

        private void btnsua_Click(object sender, EventArgs e)
        {
            if (GVSP.SelectedRows.Count > 0)
            {
                BLLSanPham sp = (BLLSanPham)GVSP.SelectedRows[0].DataBoundItem;
                sp.MaSP = txtmasp.Text;
                sp.TenSP = txttensp.Text;
                sp.MauSac = txtmausac.Text;
                sp.Gia = txtgia.Text;
                sp.ChatLieu = txtchatlieu.Text;
                sp.HinhAnh = txthinh.Text;
                sp.Idloai =cbLoaiSP.SelectedIndex;
                sp.updated = true;
                GVSP.DataSource = null;
                GVSP.DataSource = sanphams;               
            }
        }
    }
}
